package com.icez.ripple

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.RippleDrawable
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.RoundedCornerTreatment
import com.google.android.material.shape.ShapeAppearanceModel


/**
 * @author icez
 * @sample 点击效果
 */
class RippleRelativeLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : RelativeLayout(context, attrs) {

    private var mTopLeftRadius:Float = 20f
    private var mTopRightRadius:Float = 20f
    private var mBottomLeftRadius:Float = 20f
    private var mBottomRightRadius:Float = 20f
    private var mRadius:Float = 0f

    private var mStrokeColor = 0
    private var mStrokeWidth = 1f
    private var mShadowColor = 0
    private var mElevation = 0f
    private var mTintBackgroundColor = 0

    private var mRippleColor:Int = Color.GRAY

    private var mParentClipChildren:Boolean = true

    init {
        var typeArray = context.obtainStyledAttributes(attrs, R.styleable.RippleLayout)

        mRadius = typeArray.getDimensionPixelSize(R.styleable.RippleLayout_radius,0).toFloat()
        mTopLeftRadius = typeArray.getDimensionPixelSize(R.styleable.RippleLayout_topLeftRadius,0).toFloat()
        mTopRightRadius = typeArray.getDimensionPixelSize(R.styleable.RippleLayout_topRightRadius,0).toFloat()
        mBottomLeftRadius = typeArray.getDimensionPixelSize(R.styleable.RippleLayout_bottomLeftRadius,0).toFloat()
        mBottomRightRadius = typeArray.getDimensionPixelSize(R.styleable.RippleLayout_bottomRightRadius,0).toFloat()

        mStrokeColor = typeArray.getColor(R.styleable.RippleLayout_strokeColor,Color.TRANSPARENT)
        mStrokeWidth = typeArray.getDimensionPixelSize(R.styleable.RippleLayout_strokeWidth,0).toFloat()
        mShadowColor = typeArray.getColor(R.styleable.RippleLayout_shadowColor,Color.parseColor("#e6e6e6"))
        mElevation = typeArray.getDimensionPixelSize(R.styleable.RippleLayout_elevation,0).toFloat()
        mTintBackgroundColor = typeArray.getColor(R.styleable.RippleLayout_tintBackgroundColor,Color.parseColor("#ffffff"))
        mRippleColor = typeArray.getColor(R.styleable.RippleLayout_rippleColor,Color.parseColor("#80e6e6e6"))
        mParentClipChildren = typeArray.getBoolean(R.styleable.RippleLayout_parentClipChildren,true)
        typeArray.recycle()

        if(mRadius>0){
            mTopLeftRadius = mRadius
            mTopRightRadius = mRadius
            mBottomLeftRadius = mRadius
            mBottomRightRadius = mRadius
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
//        setRoundRect()
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        setRoundRect()
    }

    /**
     * 代码设置参数
     *
     * @param mRadius  圆角
     * @param mTopLeftRadius 上左圆角
     * @param mTopRightRadius 上右圆角
     * @param mBottomLeftRadius 下左圆角
     * @param mBottomRightRadius 下右圆角
     * @param mStrokeColor 线颜色
     * @param mStrokeWidth 线的宽度
     * @param mShadowColor 阴影颜色
     * @param mElevation 阴影大小
     * @param mTintBackgroundColor 背景
     * @param mRippleColor 点击颜色
     * @param mParentClipChildren 是否裁剪子布局
     */
    public fun setRoundAttrs(
        mRadius: Float? = null,
        mTopLeftRadius: Float? = null,
        mTopRightRadius:Float? = null,
        mBottomLeftRadius: Float? = null,
        mBottomRightRadius: Float? = null,
        mStrokeColor:Int? = null,
        mStrokeWidth:Float? = null,
        mShadowColor:Int? = null,
        mElevation:Float ?= null,
        mTintBackgroundColor:Int ?= null,
        mRippleColor:Int ?= null,
        mParentClipChildren:Boolean ?= null
    ) {
        if(mRadius!=null){
            this.mRadius = mRadius
        }
        if(mTopLeftRadius!=null){
            this.mTopLeftRadius = mTopLeftRadius
        }
        if(mTopRightRadius!=null){
            this.mTopRightRadius = mTopRightRadius
        }
        if(mBottomLeftRadius!=null){
            this.mBottomLeftRadius = mBottomLeftRadius
        }
        if(mBottomRightRadius!=null){
            this.mBottomRightRadius = mBottomRightRadius
        }

        if(mStrokeColor!=null){
            this.mStrokeColor = mStrokeColor
        }

        if(mStrokeWidth!=null){
            this.mStrokeWidth = mStrokeWidth

        }

        if(mShadowColor!=null){
            this.mShadowColor = mShadowColor
        }

        if(mElevation!=null){
            this.mElevation = mElevation
        }

        if(mTintBackgroundColor!=null){
            this.mTintBackgroundColor = mTintBackgroundColor

        }

        if(mRippleColor!=null){
            this.mRippleColor = mRippleColor

        }

        if(mParentClipChildren!=null){
            this.mParentClipChildren = mParentClipChildren
        }

        if (this.mRadius > 0) {
            this.mTopLeftRadius = this.mRadius
            this.mTopRightRadius = this.mRadius
            this.mBottomLeftRadius = this.mRadius
            this.mBottomRightRadius = this.mRadius
        }

        setRoundRect()
    }

    /**
     * 设置矩形
     * @sample
     * @author Icez
     */
    private fun setRoundRect(){
        if(mTintBackgroundColor==0){
            return
        }
        val shapeModel = ShapeAppearanceModel.builder()
            .setAllCorners(RoundedCornerTreatment())
            .setTopLeftCornerSize(mTopLeftRadius)
            .setTopRightCornerSize(mTopRightRadius)
            .setBottomLeftCornerSize(mBottomLeftRadius)
            .setBottomRightCornerSize(mBottomRightRadius)
            .build()


        val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
            setTint(mTintBackgroundColor)
            paintStyle = Paint.Style.FILL_AND_STROKE
            if(mStrokeWidth!=0f){
                strokeColor = createColorStateList(mStrokeColor, mStrokeColor)
                strokeWidth = mStrokeWidth
            }



            if(mElevation>0){
                shadowCompatibilityMode = MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS
                initializeElevationOverlay(context)
                setShadowColor(mShadowColor)
                elevation = mElevation
//                translationZ = elevation/2
//                translationX = elevation/3
//                translationY = elevation/3

            }
        }

        (parent as ViewGroup).clipChildren = mParentClipChildren
//        val mRippleDrawableCompat = RippleDrawableCompat(shapeModel)

        if(mElevation>0){
            background = backgroundDrawable
        }else{
            val mRippleDrawable = createColorStateList(mRippleColor, mRippleColor)?.let {RippleDrawable(it,backgroundDrawable,backgroundDrawable) }

            background = mRippleDrawable
        }
    }

    /**
     * 创建颜色列表
     * @sample
     * @author Icez
     * @param selected 选中的颜色
     * @param normal 没有选中的颜色
     */
    private fun createColorStateList(selected: Int, normal: Int): ColorStateList? {
        val colors = intArrayOf(selected, normal)
        val states = arrayOfNulls<IntArray>(2)
        states[0] = intArrayOf(android.R.attr.state_selected, android.R.attr.state_enabled)
        states[1] = intArrayOf(android.R.attr.state_enabled)
        return ColorStateList(states, colors)
    }
}